﻿using Services.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.ViewModel
{
    public class WebServices : BaseWebService, IWebServices
    {
        IRestfulHttpClient client;
        private Uri uriBase;
        public Uri UriBase
        {
            get { return uriBase; }
            set { uriBase = value; }
        }

        public WebServices(Uri baseUri)
            : base(baseUri)
        {
            UriBase = baseUri;
            client = new RestfulHttpClient(UriBase);
        }
        /// <summary>
        ///  Params n/a
        /// </summary>
        /// <param name="genre_id"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public async Task<GetAllBooks> GetAllBooksDataAsync(string device_udid, string limit, string offset)
        {
            const string serviceUrl = "book/list";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("device_udid", device_udid);
            parms.Add("limit", limit);
            parms.Add("offset", offset);

            var getString = client.GetQueryString(parms);
            GetAllBooks getData = await DoPostJonAsync<GetAllBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new GetAllBooks();

        }

        public async Task<Genres> GetGenresDataAsync()
        {
            const string serviceUrl = "genre/list";
            Genres getData = await DoPostJonAsync<Genres>(serviceUrl, string.Empty);
            if (getData != null)
                return getData;
            else
                return new Genres();
        }

        public async Task<BuyBooks> GetBuyBooksAsync(string book_id,string device_udid)
        {
            const string serviceUrl = "book/buy";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("book_id", book_id);
            parms.Add("device_udid", device_udid);

            var getString = client.GetQueryString(parms);
            BuyBooks getData = await DoPostJonAsync<BuyBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new BuyBooks();

        }

        public async Task<GetBookDetail> GetBookDetailAsync(string book_id, string device_udid)
        {
            const string serviceUrl = "book/detail";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("book_id", book_id);
            parms.Add("device_udid", device_udid);


            var getString = client.GetQueryString(parms);
            GetBookDetail getData = await DoPostJonAsync<GetBookDetail>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new GetBookDetail();
        }
        /// <summary>
        /// Get All Hot Books Async
        /// </summary>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<HotBooks> GetAllHotBooksAsync(string limit)
        {

            const string serviceUrl = "book/hot";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("limit", limit);

            var getString = client.GetQueryString(parms);
            HotBooks getData = await DoPostJonAsync<HotBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new HotBooks();
        }

        public async Task<NewestBooks> GetWestBooksAsync(string limit)
        {
            const string serviceUrl = "book/newest";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("limit", limit);

            var getString = client.GetQueryString(parms);
            NewestBooks getData = await DoPostJonAsync<NewestBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new NewestBooks();
        }
        /// <summary>
        /// Get Recommend Books
        /// </summary>
        /// <param name="book_id"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<RecommendBooks> GetRecommendBooks(string book_id,string limit)
        {
            const string serviceUrl = "book/recommend";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("book_id", book_id);
            parms.Add("limit", limit);

            var getString = client.GetQueryString(parms);
            RecommendBooks getData = await DoPostJonAsync<RecommendBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new RecommendBooks();
        }
        /// <summary>
        /// Get Regist Device
        /// </summary>
        /// <param name="device_udid"></param>
        /// <returns></returns>
        public async Task<RegisterDevice> GetRegistDevice(string device_udid)
        {
            const string serviceUrl = "device/regist";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("device_udid", device_udid);

            var getString = client.GetQueryString(parms);
            RegisterDevice getData = await DoPostJonAsync<RegisterDevice>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new RegisterDevice();
        }
        /// <summary>
        /// Get Search Data Books Async
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="type"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<SearchBooks> GetSearchDataBooksAsync(string limit, string offset, string type, string keyword)
        {
            const string serviceUrl = "book/search";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("limit", limit);
            parms.Add("offset", offset);
            parms.Add("type", type);
            parms.Add("keyword", keyword);

            var getString = client.GetQueryString(parms);
            SearchBooks getData = await DoPostJonAsync<SearchBooks>(serviceUrl, getString);
            if (getData != null)
                return getData;
            else
                return new SearchBooks();
        }

        /// <summary>
        /// Get Series Books Async
        /// </summary>
        /// <param name="genre_id"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public async Task<Series> GetSeriesBooksAsync(string genre_id, string limit, string offset)
        {
            string serviceUrl = string.Format("series/list?genre_id={0}&limit={1}&offset={2}",genre_id,limit,offset);
            
            Series getData = await DoPostJonAsync<Series>(serviceUrl, string.Empty);
            if (getData != null)
                return getData;
            else
                return new Series();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="series_id"></param>
        /// <returns></returns>
        public async Task<SeriesBooksDetail> GetSeriesBookDetailAsync(string series_id)
        {
            const string serviceUrl = "series/detail";
            Dictionary<string, string> parms = new Dictionary<string, string>();
            parms.Add("series_id", series_id);
           

            var getString = client.GetQueryString(parms);
            try
            {
                SeriesBooksDetail getData = await DoPostJonAsync<SeriesBooksDetail>(serviceUrl, getString);
                if (getData != null)
                    return getData;
                else
                    return new SeriesBooksDetail();
            }
            catch(Exception e)
            {                
                return new SeriesBooksDetail();
            }

         
        }


        public async Task<Series> GetSeriesBooksAsync(string genre_id)
        {
            string serviceUrl = "series/list?genre_id=" + genre_id;
            Series getData = await DoPostJonAsync<Series>(serviceUrl, string.Empty);
            if (getData != null)
                return getData;
            else
                return new Series();
        }
    }
}
